package com.awifi.com.thread.communication;


/**
 * @description: 蛋糕生产商，给消费者提供蛋糕
 * @author: marin
 * @time: 2020/6/29 16:18
 */
public class Producer extends Thread {

    private Cake cake;
    public Producer(String name,Cake cake){
        super(name);
        this.cake = cake;
    }

    @Override
    public void run() {
        while (true){
            synchronized (cake) {
                if(cake.isFlag()){
                 try {
                     cake.wait();
                 }catch (InterruptedException e){
                     e.printStackTrace();
                 }
                }
                System.out.println("需要准备糕点");
                try{
                    Thread.sleep(3000);
                }catch (InterruptedException e) {
                    e.printStackTrace();
                }
                cake.setFlag(true);
                System.out.println("糕点准备完成");
                cake.notify();
            }
        }
    }
}
